package com.cfets.annualAssessment.service;


import com.cfets.annualAssessment.entity.Attribute;
import com.cfets.annualAssessment.mapper.AttributeMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liusonglin on 2016/11/21.
 */
@Service
public class AttributeService {
    @Autowired
    private AttributeMapper attributeMapper;

    public List<Attribute> getAttributePage(Attribute attribute) {
        Page<?> page = PageHelper.startPage(attribute.getPage(), attribute.getRows());
//        List<Attribute> attributes = new ArrayList<Attribute>();
        Example example=new Example(Attribute.class);
        example.setOrderByClause("post_time DESC");
        Example.Criteria criteria =example.createCriteria();
        if(attribute.getType()==1||attribute.getType()==2||attribute.getType()==3){
            criteria.andEqualTo("type",attribute.getType());
        }
        if(!StringUtils.isEmpty(attribute.getValue())){
            criteria.andLike("value","%"+attribute.getValue()+"%");
        }
        List<Attribute> attributes = attributeMapper.selectByExample(example);
        attribute.setTotalCount(page.getTotal());
        return attributes;
    }

    public boolean newAttribute(Attribute attribute) {
        return attributeMapper.insertSelective(attribute)>0;
    }

    public boolean updateAttribute(Attribute attribute) {
        return attributeMapper.updateByPrimaryKey(attribute)>0;
    }

    public boolean deleteAttributeById(int id) {
        return attributeMapper.deleteByPrimaryKey(id)>0;
    }
}
